package com.zhongxiangspace.controller.pagemanage;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.google.gson.Gson;
import com.zhongxiangspace.entity.*;
import com.zhongxiangspace.result.ExceptionResult;
import com.zhongxiangspace.result.NormalResult;
import com.zhongxiangspace.result.Response;
import com.zhongxiangspace.result.Result;
import com.zhongxiangspace.service.CountyService;
import com.zhongxiangspace.service.OfficeBuildingService;
import com.zhongxiangspace.service.StationBranchService;
import com.zhongxiangspace.tools.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * 工位
 */
@Controller
public class StationBranchController {
    @Autowired
    private CountyService countyService;

    @Autowired
    private StationBranchService stationBranchService;
    @Autowired
    private OfficeBuildingService officeBuildingService;


    /**
     * 逻辑删除
     */
    @ResponseBody
    @PostMapping("/officeBuilding/stationBranch/attribute-delete.do")
    public Result deleteAttribute(Integer id){
        try {
            boolean flag=this.stationBranchService.deleteAttribute(id);
            return  new NormalResult(flag);
        }catch (Exception e){
            return  new ExceptionResult(e);
        }
    }


    /**
     * 保存-添加工位房源
     * @return
     */
    @ResponseBody
    @PostMapping("/officeBuilding/stationBranch/attribute-insert.do")
    public Result  attribute(StationHouse stationHouse){
        try {
            stationHouse.setCreate_time(DateUtil.getDateTime());
            stationHouse.setModify_time(DateUtil.getDateTime());
            boolean flag=this.stationBranchService.attributeinsert(stationHouse);
            return  new NormalResult("SUCCESS");
        }catch (Exception e){
            return  new ExceptionResult(e);
        }
    }

    /**
     * 工位房源列表
     * @param model
     * @param id
     * @return
     */
    @GetMapping("/officeBuilding/stationBranch/attribute.do")
    public String  attribute(Model model,Integer id){
        StationBranch stationBranch = this.stationBranchService.queryOne(id);
        model.addAttribute("data",stationBranch);
       List<StationHouse>  stationHouse=this.stationBranchService.queryStationHouse(id);
        model.addAttribute("stationHouse",stationHouse);
        return "basics/station/stationBranch_attribute";
    }
    /**
     * 更新
     * @param stationBranch
     * @return
     */
    @ResponseBody
    @PostMapping("/officeBuilding/stationBranch/update.do")
    public Result update(StationBranch stationBranch) {
        try {
            stationBranch.setModify_time(DateUtil.getDateTime());
            boolean flag=this.stationBranchService.updata(stationBranch);
            return new NormalResult("SUCCESS");
        } catch (Exception e) {
            return new ExceptionResult(e);
        }
    }

    /**
     * 编辑
     *
     * @param request
     * @param model
     * @param id
     * @param type
     * @return
     */
    @GetMapping("/officeBuilding/stationBranch/edit.do")
    public String edit(HttpServletRequest request, Model model, Integer id, String type) {
        List<StationIcon> stationIconList = this.stationBranchService.queryIcon(null);
        model.addAttribute("stationIconList", stationIconList);
        List<County> countyList = countyService.queryAllType(0);
        model.addAttribute("data", countyList);
        StationBranch stationBranch = this.stationBranchService.queryOne(id);
        model.addAttribute("object", stationBranch);
        String gson = new Gson().toJson(stationBranch);
        model.addAttribute("stationBranchJson", gson);
        //获取区域下所有商圈
        List<County> tradeAreaList = countyService.queryAllType(stationBranch.getCounty_id());
        model.addAttribute("tradeAreaList", tradeAreaList);
        //获取商圈下所有楼盘
        QueryAnalyzer queryAnalyzer = new QueryAnalyzer();
        queryAnalyzer.setTrade_area_id(stationBranch.getTrade_area_id());
        List<OfficeBuildings> officeBuildingsList = this.officeBuildingService.queryByQueryAnalyzer(queryAnalyzer);
        model.addAttribute("officeBuildingsList", officeBuildingsList);
        if (type != null && !type.isEmpty() && type.equals("SEE")) {
            return "basics/station/stationBranch_see";
        } else {
            return "basics/station/stationBranch_update";
        }
    }


    /**
     * 异步删除 data
     *
     * @return
     */
    @ResponseBody
    @PostMapping("/officeBuilding/stationBranch/delete.do")
    public Response delete(Integer id) {
        try {
            boolean flag = this.stationBranchService.delete(id);
            return new Response(0, "SUCCESS", 0, null);
        } catch (Exception e) {
            e.printStackTrace();
            return new Response(1, "ERROR", 0, null);
        }
    }

    /**
     * 异步热门和非热门
     *
     * @return
     */
    @ResponseBody
    @PostMapping("/officeBuilding/stationBranch/statusHot.do")
    public Response statusHot(Integer id) {
        try {
            StationBranch stationBranch = this.stationBranchService.queryOne(id);
            stationBranch.setModify_time(DateUtil.getDateTime());
            if (stationBranch != null && stationBranch.getHotCode().equals(0)) {
                stationBranch.setHotCode(1);
                boolean flag = this.stationBranchService.updataStateHot(stationBranch);
            } else if (stationBranch != null && stationBranch.getHotCode().equals(1)) {
                stationBranch.setHotCode(0);
                boolean flag = this.stationBranchService.updataStateHot(stationBranch);
            }
            return new Response(0, "SUCCESS", 0, null);
        } catch (Exception e) {
            e.printStackTrace();
            return new Response(1, "ERROR", 0, null);
        }
    }


    /**
     * 异步修改上下线 data
     *
     * @return
     */
    @ResponseBody
    @PostMapping("/officeBuilding/stationBranch/status.do")
    public Response status(Integer id) {
        try {
            StationBranch stationBranch = this.stationBranchService.queryOne(id);
            stationBranch.setModify_time(DateUtil.getDateTime());
            if (stationBranch != null && stationBranch.getShowCode().equals(0)) {
                stationBranch.setShowCode(1);
                boolean flag = this.stationBranchService.updataStates(stationBranch);
            } else if (stationBranch != null && stationBranch.getShowCode().equals(1)) {
                stationBranch.setShowCode(0);
                boolean flag = this.stationBranchService.updataStates(stationBranch);
            }
            return new Response(0, "SUCCESS", 0, null);
        } catch (Exception e) {
            e.printStackTrace();
            return new Response(1, "ERROR", 0, null);
        }
    }


    /**
     * 房源list
     *
     * @param request
     * @param model
     * @return
     */
    @GetMapping("/officeBuilding/stationBranch/list.do")
    public String list(HttpServletRequest request, Model model) {
        List<County> tradeAreaList = countyService.queryAllType(null);
        model.addAttribute("data", tradeAreaList);
        String gson = new Gson().toJson(tradeAreaList);
        model.addAttribute("dataJson", gson);
        return "basics/station/stationBranch_list";
    }

    /**
     * 异步
     *
     * @param request
     * @return
     */
    @ResponseBody
    @GetMapping("/officeBuilding/stationBranch/ajaxList.do")
    public Response ajaxList(HttpServletRequest request, QueryAnalyzer queryAnalyzer) {
        PageHelper.startPage(queryAnalyzer.getPage(), queryAnalyzer.getLimit(), "modify_time DESC  ");
        Page<Map<String, Object>> stationBranchList = (Page<Map<String, Object>>) this.stationBranchService.queryAll(queryAnalyzer);
        try {
            return new Response(0, "222", stationBranchList.getTotal(), stationBranchList.getResult());
        } catch (Exception e) {
            return new Response(1200, "ERROR", 2555, null);
        }
    }


    /**
     * 添加
     *
     * @param request
     * @param model
     * @return
     */
    @GetMapping("/officeBuilding/stationBranch/add.do")
    public String add(HttpServletRequest request, Model model) {
        List<County> tradeAreaList = countyService.queryAllType(0);
        model.addAttribute("data", tradeAreaList);
        String type = "";
        List<StationIcon> stationIconList = this.stationBranchService.queryIcon(type);
        model.addAttribute("stationIconList", stationIconList);
        return "basics/station/stationBranch_add";
    }


    @ResponseBody
    @PostMapping("/officeBuilding/stationBranch/insert.do")
    public Result insert(StationBranch stationBranch) {
        try {
            stationBranch.setModify_time(DateUtil.getDateTime());
            stationBranch.setCreate_time(DateUtil.getDateTime());
            boolean flag = this.stationBranchService.insert(stationBranch);
            return new NormalResult("SUCCESS");
        } catch (Exception e) {
            return new ExceptionResult(e);
        }
    }


}
